package com.simran.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	//initialise the text editors
	private EditText textCel;
	private EditText textFar;
	private EditText textKel;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//matching the editText objects to the correlating ones in gui
		textCel = (EditText) findViewById(R.id.editText1);
		textFar = (EditText) findViewById(R.id.editText2);
		textKel = (EditText) findViewById(R.id.editText3);
	}
	public void clickHandler(View v) {
		switch (v.getId()) {
		case R.id.button1:
			if (textCel.getText().length() == 0 && textFar.getText().length() == 0 && textKel.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
				return;
			}
			if (textCel.getText().length() > 0) {
				float inputValueCel = Float.parseFloat(textCel.getText().toString());
				textFar.setText(String.valueOf(convertCelToFar(inputValueCel)));
				textKel.setText(String.valueOf(convertCelToKel(inputValueCel)));
			}
			else if (textFar.getText().length() > 0) {
				float inputValueFar = Float.parseFloat(textFar.getText().toString());
				textCel.setText(String.valueOf(convertFarToCel(inputValueFar)));
				textKel.setText(String.valueOf(convertFarToKel(inputValueFar)));
			}

			else if (textKel.getText().length() > 0) {
				float inputValueKel = Float.parseFloat(textKel.getText().toString());
				textCel.setText(String.valueOf(convertKelToCel(inputValueKel)));
				textFar.setText(String.valueOf(convertKelToFar(inputValueKel)));
			}
			break;

		case R.id.button2:
			textCel.setText("");
			textFar.setText("");
			textKel.setText("");
			break;
		}
	}

	//converting from fahrenheit
	private float convertFarToCel(float far) {
		return ((far - 32) * 5 / 9);
	}
	private float convertFarToKel(float far) {
		return (5 / 9 * (far -32) +273 );
	}

	//Converts from celsius
	private float convertCelToFar(float cel) {
		return ((cel * 9) / 5) + 32;
	}

	private float convertCelToKel(float cel) {
		return (cel + 273);
	}

	//converting from kelvin
	private float convertKelToFar(float kel) {
		return ((kel - 273) * 9 / 5 ) + 32;
	}
	private float convertKelToCel(float kel) {
		return (kel - 273);
	}
}
